<?php

	function eventitemGET($request) {
		$eventItem = new EventItem();
		$result = null;
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventItem->setEventItemId($request->getArgs['id']);
			
			$session = unserialize($_SESSION['smarthotel']['session']);
			$user = $session->getUser();			
			$eventItem->getEventItemUser()->getEventUser()->setUser($user);
					
			$eventItemBusiness = BusinessFactory::factory('EventItemBusiness');
			$result = $eventItemBusiness->select($eventItem);
			
			if ($result != NULL) {
			
				if(is_array($result)) {
					if (sizeof($result) == 1) {
						header("Status: 200 Success", true, 200);
						return $result[0];
					}
					else {
						header("Status: 404 Not Found", true, 404);
					}
				} else {
					header("Status: 500 Internal Server Error", true, 500);
				}
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			} 
		}
		else{
			header("Status: 400 Bad Request", true, 400);
		}
		
		return $result;
	}
	
	function eventitemuserGET($request){
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventItem = new EventItem();
			$eventItem->setEventItemId($request->getArgs['id']);
									
			$eventItemBusiness = BusinessFactory::factory('EventItemUserBusiness');
			$result = $eventItemBusiness->selectEventItemAttendant($eventItem);
			
			if ($result != NULL) {
				header("Status: 200 Ok", true, 201);
				//TODO checar qual location seria retornado
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
		
		return $result;
	}
	
	function eventitemuserPOST($request){
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventItem = new EventItem();
			$eventItem->setEventItemId($request->getArgs['id']);
			
			//TODO pensar em como viria o isSpeaker e o userRegistrationCode
			$eventItem->getEventItemUser()->getEventUser()->setUserRegistrationCode($request->postArgs['userRegistrationCode']);
			$eventItem->getEventItemUser()->setIsUserSpeaker($request->postArgs['isUserSpeaker']);
						
			$eventItemBusiness = BusinessFactory::factory('EventItemBusiness');
			$result = $eventItemBusiness->saveEventItemAttendant($eventItem);
			
			if ($result) {
				header("Status: 201 Created", true, 201);
				//TODO checar qual location seria retornado
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
	
	function eventitemuserDELETE($request){
		
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventItem = new EventItem();
			$eventItem->setEventItemId($request->getArgs['id']);
			
			//TODO pensar em como viria o userRegistrationCode
			$eventItem->getEventItemUser()->getEventUser()->setUserRegistrationCode($request->deleteArgs['userRegistrationCode']);
			
			//$eventItemBusiness = BusinessFactory::factory('EventItemBusiness');
			$eventItemUserBusiness = BusinessFactory::factory('EventItemUserBusiness');
			$result = $eventItemUserBusiness->deleteEventItemAttendant($eventItem);
			
			if ($result) {
				header("Status: 200 Ok", true, 200);
				//TODO checar qual location seria retornado
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
		
		return $result;
	}
	
	function eventitemnotificationGET($request) {
		$result = null;	
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventItemId = $request->getArgs['id'];
			
			$eventNotificationBusiness = BusinessFactory::factory('EventNotificationBusiness');
			$result = $eventNotificationBusiness->selectEventNotificationByEventItemId($eventItemId);
			
			if ($result != NULL) {
			
				if(is_array($result) && sizeof($result) > 0) {
						
					header("Status: 200 Success", true, 200);
					
				} else {
					header("Status: 404 Not Found", true, 404);
				} 
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			} 
			return $result;
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}	
	
	function eventitemnotificationPOST($request) {
		$result = null;	
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
						
			$notificationJSON = json_decode(utf8_decode($request->postArgs['notification']));
			$notification = new Notification();
			$notification->JSONToObject($notificationJSON);
			
			$eventNotificationBusiness = BusinessFactory::factory('EventNotificationBusiness');
			$result = $eventNotificationBusiness->saveNotification($notification);
			
			if ($result != -1) {
				header("Status: 201 Created", true, 201);
				header("Location: " . $request->getActualUrl() . $result);
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}	
	
	function eventitemcommentGET($request) {
		$result = null;	
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventItemId = $request->getArgs['id'];
			
			$eventItemCommentBusiness = BusinessFactory::factory('EventItemCommentBusiness');
			$result = $eventItemCommentBusiness->selectEventItemCommentByEventItemId($eventItemId);
			
			if ($result != NULL) {
			
				if(is_array($result) && sizeof($result) > 0) {
						
					header("Status: 200 Success", true, 200);
					
				} else {
					header("Status: 404 Not Found", true, 404);
				} 
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			} 
			return $result;
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}	
	
	function eventitemcommentPOST($request) {
		$result = null;	
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
						
			$commentJSON = json_decode(utf8_decode($request->postArgs['comment']));
			$comment = new EventItemComment();
			$comment->JSONToObject($commentJSON);
			
			$eventItemCommentBusiness = BusinessFactory::factory('EventItemCommentBusiness');
			$result = $eventItemCommentBusiness->saveComment($comment);
			
			if ($result != -1) {
				header("Status: 201 Created", true, 201);
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
	
	function eventitemmaterialGET($request) {
		$result = null;	
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$eventItemId = $request->getArgs['id'];
			
			$eventItemMaterialBusiness = BusinessFactory::factory('EventItemMaterialBusiness');
			$result = $eventItemMaterialBusiness->selectEventItemMaterialByEventItemId($eventItemId);
			
			if ($result != NULL) {
			
				if(is_array($result) && sizeof($result) > 0) {
						
					header("Status: 200 Success", true, 200);
					
				} else {
					header("Status: 404 Not Found", true, 404);
				} 
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
			return $result;
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}	
	
	function eventitemmaterialPOST($request) {
		$result = null;	
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
						
			$materialJSON = json_decode(utf8_decode($request->postArgs['material']));
			$material = new EventItemMaterial();
			$material->JSONToObject($materialJSON);
			$material->setMaterialTempName($request->fileArgs['file']['tmp_name']);
			$material->setMaterialFileExtension(getFileExtension($request->fileArgs['file']['name']));
			
			$eventItemMaterialBusiness = BusinessFactory::factory('EventItemMaterialBusiness');
			$result = $eventItemMaterialBusiness->saveMaterial($material);
			
			if ($result != -1) {
				header("Status: 201 Created", true, 201);
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
	

?>